/* JavaScript Metadata Dictionary Initialization */

/* Define metadata dictionary values below. This can be done in the following ways:
	- Directly in the presentation layer or on the page 
	- Automatically, using autoassignments from page-specific content (DOM, URL query string, cookies, etc)
*/
var tmpPage1={
	"page.hierarchy":"level1:level2:level3:level4",
	"page.name":"This is my page!",
	"page.subname":"tab1",
	"user.segment":"small business",
	"page.promo":["gQP|cid","tSub(arg,'_',2)"]
}



var _initJSMDconfig={
	/*  Basic config - simple skeleton metadata hierarchy. All page, session, user, and other pertinent metadata will be stored in this container
		and leveraged by various analytics solutions. */
	basic_config:  "{page:{},product:{},user:{},promo:{}};",
	version: "20100314",
	
	/* Vendor-neutral data collection routines go below. Make certain all the data collection routines start with a lower-case g */
	mdCollection: {
		gQP: function(param,url){var b,m,l,k,j,i,h,g,f,e,d,c;b=(url!=null?url:window.location.href);b=b.split('?');m=b.length;if(m>1){for(l=0;l<m;l++){i=b[l].split('&');j=i.length;for(k=0;k<j;k++){g=i[k].split('=');if(g[0]==param)h=g[1]}f=b[l].split('%26');e=f.length;for(d=0;d<e;d++){c=f[d].split('%3d');if(c[0]==param)h=c[1]}}}h=h?h:'';return h},
		gCook: function(param) { return this.common.getcookie(param);},
		gCookClr: function(param) { var r=this.common.getcookie(param);this.common.setcookie(param,"-","-1"); return r},
		gMeta: function(tag) {},
		gDOM: function(selector) {}
	},
	
	/* Vendor-neutral data transformation routines go below.  They will be added to the root context of the closure. */
	mdTransform: {
		tCase: function(arg) { 	return arg.toLowerCase();		// Case transformation function
		},
		tTrim: function(arg) { return arg.replace(/\s+/g," ").replace(/^\s/,"").replace(/\s$/,""); // Process whitespaces - trim & pump out extra whitespaces 
		},
		tSub: function(arg,delimiter,i) { var r=arg; try{r=arg.split(delimiter)[i];}catch(err) {}; return r;
		},
		tAll: "tCase|tTrim" // The "tAll" transform is a special transform and will be applied to all functions automatically
							// Note, you can only have a single ALL function - multiple "all" functions are not supported.
	},
	
	/* Other built-in functions: 
		function processConfig(configObject);
			- Allows the user to pass in a configuration JSON object with absolute metadata values, 
	*/
	/* Used in configuration shorthand - default delimiters */
	transform_delimiter: "|", argument_delimiter:",",
	common: {
		"getcookie":function(k){var c=' '+document.cookie,s=c.indexOf(' '+k +'='),e=s<0?s:c.indexOf(';',s),v=s<0?'':c.substring(s+2 +k.length,e<0?c.length:e);return v},
		"setcookie":function(k,v,e,d){var exp=new Date((new Date().getTime())+e*86400000); document.cookie=k+"="+escape(v)+ (e!=null?"; expires="+exp.toGMTString():"") +"; path=/;"+(d!=null?" domain="+d:"");}
	}

};	


function _initJSMD() {
	
	var t=window._initJSMDconfig,i,i2,_jsmd,alltrfm=[],t2,t3,a1;
	_jsmd=eval("(function(){ return "+t.basic_config+"})();");
	_jsmd.about={version:t.version};
	for(i in t.mdCollection) {_jsmd[i]=t.mdCollection[i];}
	_jsmd.trfm={};
	t2=t.mdTransform;
	for(i in t2)  {
		if(typeof(t2[i])=='string')  {a1=t2[i].split("|"); for(i2=a1.length-1;i2>=0;i2--) alltrfm.push(t2[a1[i2]]); _jsmd[i]=function(arg) {var targ=arg,i; for(i=alltrfm.length-1;i>=0;i--) targ=alltrfm[i](targ); return targ;}; }
		else {
			t3=t2[i];_jsmd[i]=t3;
		}
	}
	_jsmd.loadValues=function(config) {
		var k,v,v1;
		for(k in config) {
			v=config[k];
			if(typeof(v)=='string') v=getStr2(v);
			if(typeof(v)=='array') { v1=getStr2(v[0]); v=v[1].split("arg").join("'"+v1+"'"); }
			eval("this."+k+"='"+v.replace(/[\']/,"\\'")+"'"); 
		}
		function getStr2(str2) {
			var t=this.transform_delimiter,t2,val;
			// try {
				alert(str2);
				if(str2.indexOf(t)!=-1) {
					t2=str2.split(t);
					val=this[t2[0]](t2[1]);
				} else val=str2;
			// } catch(err) {};
			return val;
		}
	}
	return _jsmd;	
}
var x=_initJSMD();x.loadValues(tmpPage1)